home *** CD-ROM | disk | FTP | other *** search
/ NOVA - For the NeXT Workstation / NOVA - For the NeXT Workstation.iso / SourceCode / AtYourService / MyDelegate.m < prev    next >
Text File  |  1992-12-13  |  7KB  |  218 lines

  1.  
  2. /* This Object is the application's delegate. It is responsible for setting 
  3.  * itself up as the Delegate for Services requests specific to this 
  4.  * application. It interfaces with another object which handles services 
  5.  * preferences
  6.  */
  7.  
  8. #import "ServicesHandler.h"
  9. #import "DefaultHandler.h"
  10. #import "MyDelegate.h"
  11. #import <defaults/defaults.h>
  12. #import <appkit/nextstd.h>
  13. #import <appkit/Application.h>
  14. #import <appkit/Listener.h>
  15. #import <appkit/Window.h>
  16. #import <appkit/Pasteboard.h>
  17. #import <NXCType.h>
  18. #import <strings.h>
  19. #import <mach.h>
  20.  
  21.  
  22. @implementation MyDelegate
  23.  
  24. /* Setup Services Delegate and Initialize Preferences Panel */
  25.  
  26. - appDidInit:sender
  27. {
  28.     [[NXApp appListener] setServicesDelegate:self];
  29.     
  30.     [servicesHandler prefSetup:self];
  31.     [defaultHandler readDefaults:self];
  32.     return self;
  33. }
  34.  
  35. /* Brings up the Info panel.   Not done on startup because it's in a separate
  36.  * interface file. Saves startup time for the user if we do this when they ask
  37.  * for it, and not before.
  38.  */
  39. - infoPanel:sender
  40. {
  41.     if( ! infoPanel ){
  42.         [NXApp loadNibSection:"Information.nib" owner:self];
  43.     }
  44.     [infoPanel makeKeyAndOrderFront:sender];
  45.     return self;    
  46. }
  47.  
  48. /*
  49.  * Brings up the Help panel (as above)
  50.  */
  51. - helpPanel:sender
  52. {
  53.     if( ! helpPanel ){
  54.         [NXApp loadNibSection:"Information.nib" owner:self];
  55.     }
  56.     [helpPanel makeKeyAndOrderFront:sender];
  57.     return self;    
  58.  
  59. }
  60.  
  61. /*  This method sends the data from the sender's "preferred" pasteboard
  62.  *  (the first on the "types" list) through the Unix sort utility and 
  63.  *  back to both the Ascii and TabText pasteboards.
  64.  */
  65.  
  66. - sort:(id)pasteboard
  67.     userData:(const char *)sortArgs
  68.     error:(char **)errorMessage
  69. {
  70.     int length, lengths[2], storedOutput[2], thisPboard, currentType;
  71.     const char *data;
  72.     const char *const *ptypes;
  73.     char *myTypes[2];
  74.     char *outputPboards[2];
  75.     
  76.     myTypes[0] = (char *) NXAsciiPboardType;             storedOutput[0] = 0;
  77.     myTypes[1] = (char *) NXTabularTextPboardType;       storedOutput[1] = 0;
  78.     
  79.     ptypes=[pasteboard types];
  80.     
  81.    /* calculate over all pasteboardtypes passed in */
  82.     for (currentType=0; ptypes[currentType] ; currentType++ )
  83.         if (    ( !strcmp(ptypes[currentType],NXTabularTextPboardType) ) ||
  84.                 ( !strcmp(ptypes[currentType],NXAsciiPboardType) )) {
  85.         FILE *temp, *process;
  86.         int i;
  87.         char filename[25], command[256];
  88.         char *outbuffer;
  89.         
  90.         [pasteboard readType:ptypes[currentType] data:&data length:&length];
  91.         thisPboard= ( strcmp(ptypes[currentType],myTypes[0]) );
  92.         
  93.         outputPboards[thisPboard]=malloc(length+2);
  94.         outbuffer=outputPboards[thisPboard];
  95.         
  96.         strncpy(outbuffer, data, length);
  97.         if ( outbuffer[length-1] != '\n' ) {
  98.         
  99.           /* Sort data needs a newline at the end,  add one */
  100.             outbuffer[length] = '\n';
  101.             outbuffer[length+1] = '\0';
  102.         }
  103.         else outbuffer[length]='\0';
  104.  
  105.       /* Make a temporary file to give to Sort */
  106.         
  107.         strcpy(filename,"/tmp/file000000Selection" );
  108.         NXGetTempFilename(filename,9);
  109.         temp=fopen(filename,"w");
  110.         fprintf(temp,outbuffer);
  111.         fclose(temp);
  112.         
  113.       /* Call Sort using popen(),  The userData supplied has the
  114.        * arguments for Sort (see "services" file).
  115.        */
  116.         sprintf(command,"sort %s %s %s\n",
  117.                 [defaultHandler sortFields],sortArgs,filename);
  118.         process=popen(command,"r");
  119.         
  120.         for (i=0; ((outbuffer[i]=getc(process)) != EOF); i++)
  121.             outbuffer[i+1] = '\0';
  122.             
  123.         lengths[thisPboard] = i;
  124.         storedOutput[thisPboard] = 1;   
  125.         
  126.         unlink(filename);
  127.         pclose(process);
  128.     }
  129.     
  130.     if (!storedOutput[0] && !storedOutput[1]) 
  131.         *errorMessage = "No ASCII text found in your selection.";
  132.     else  {
  133.         [pasteboard declareTypes:&myTypes[0] num:2 owner:self];
  134.         
  135.       /* Place the results on the corresponding pasteboards. The two pboards
  136.        * are somewhat interchangeable,  if one result doesn't exist,  send the
  137.        * other result in its place.  A service must provide all pboard types 
  138.        * mentioned as "Return Types" in the services section. 
  139.        */
  140.         
  141.         [pasteboard writeType:myTypes[0] 
  142.             data: storedOutput[0] ? outputPboards[0] : outputPboards[1]
  143.             length:storedOutput[0] ? lengths[0] : lengths[1]];
  144.         [pasteboard writeType:myTypes[1] 
  145.             data: storedOutput[1] ? outputPboards[1] : outputPboards[0]
  146.             length:storedOutput[1] ? lengths[1] : lengths[0]];
  147.         if (storedOutput[0]) free(outputPboards[0]);
  148.         if (storedOutput[1]) free(outputPboards[1]);
  149.         }
  150.  
  151.     return self;
  152. }
  153.  
  154.  
  155.  
  156. /* Another simple service that capitalizes words */
  157.  
  158. - capitalize:pb userData:(const char *)userData error:(char **)errorMessage
  159. {
  160.     char *data;
  161.     int length;
  162.     const char *const *types;
  163.     int hasAscii, i;
  164.  
  165.     types = [pb types];
  166.     hasAscii=0;
  167.     for (i=0; !hasAscii && types[i] ; i++) 
  168.         if (!strcmp(types[i], NXAsciiPboardType)) hasAscii=1;
  169.     if (hasAscii) {
  170.         [pb readType:NXAsciiPboardType data:&data length:&length];
  171.         if (data && length) {
  172.             int First, i;
  173.             char *returnData;
  174.             
  175.             returnData=malloc(length+1);
  176.             First=TRUE;
  177.             data = strncpy(returnData, data, length);
  178.             returnData[length] = '\0';
  179.             for (i=1;i<=length;i++) {
  180.                 if ( NXIsLower(*data) && First ) {
  181.                         *data = NXToUpper(*data);
  182.                         First=FALSE;
  183.                 }
  184.                 else if (! NXIsAlpha(*data)) First=TRUE;
  185.                     else First=FALSE;
  186.                 data++;
  187.             }
  188.             [pb declareTypes:&NXAsciiPboardType num:1 owner:self];
  189.             [pb writeType:NXAsciiPboardType data:returnData length:length];
  190.             free(returnData);
  191.         } 
  192.         else *errorMessage = "No data in your selection.";
  193.     } 
  194.     else *errorMessage = "No ASCII text found in your selection.";
  195.  
  196.     return self;
  197. }
  198.  
  199. /*  This is an example of a Service that take no input.  It simply returns
  200.  *  the current date and time.
  201.  */
  202.  
  203. - timestamp:(id)pasteboard
  204.     userData:(const char *)sortArgs
  205.     error:(char **)msg
  206. {
  207.     long timenum;
  208.     char *data;
  209.  
  210.     timenum=time(NULL);
  211.     data=asctime(localtime(&timenum));
  212.     [pasteboard declareTypes:&NXAsciiPboardType num:1 owner:self];
  213.     [pasteboard writeType:NXAsciiPboardType data:data length:25];
  214.     return self;
  215. }
  216.  
  217. @end
  218.